<?php
/**
 * 商铺审核控制器
 * @author chenyangbj8826
 *
 */
class Controller_Admin_Audit extends Controller_Template {
	
	private $page_per_count = 10; //每页显示条数
	public function __construct($request,$response)
	{
		parent::__construct($request,$response);
		$this->template	= ('smarty:admin/shop/audit');		
	}
	
	/**
	 * 界面展示
	 */
	public function action_index()
	{
		//如何查询条件为空则为查询全部
		$query_type = $this->request->query('query') ? $this->request->query('query') :  'all'; 		
		$shop_name = (string) $this->request->query('search');
		$page_no = intval($this->request->query('page')) ? intval($this->request->query('page')) : 1;
		$start_num = ($page_no -1) * $this->page_per_count;

		//获得商铺数组
		$query = array(); 	 
		$query['order_by'] = array('apply_time' => 'DESC');
		($query_type!='all') && $query['shop_status'] = $query_type; 
		strlen(trim($shop_name)) && $query['shop_name'] = trim($shop_name); 
		$arr_shops=$this->get_shops($query,$start_num,$this->page_per_count);
		
		$this->template->set('pagination', Pagination::factory(array('total_items'=> $arr_shops['count'],'items_per_page'=>$this->page_per_count)));
		$this->template->set('pagecount',$arr_shops['count']);
		$this->template->set('current_query',$query_type);
		$this->template->set('shops',$arr_shops['list']);
		$this->template->set('shop_name',$shop_name);
		$this->template->set('close_reasons',$this->get_reasons());
	}
	
	/**
	 * 关闭店铺
	 */
	public function action_close($shop_id,$reason_key)
	{
		$this->set_shop_status($shop_id, $this->get_status_name(Kohana::config('shop.open_status.close')),$reason_key);		
	}
	
	/**
	 * 重新打开店铺
	 */
	public function action_reopen($shop_id)
	{
		$this->set_shop_status($shop_id, $this->get_status_name(Kohana::config('shop.open_status.open')));
	}
    
	/**
	 * 设置店铺的状态
	 * @param string $shop_id 商铺id
	 * @param string $status  商铺状态
	 * @param string $reason_key  关闭理由
	 */
	private  function set_shop_status($shop_id,$status,$reason_key=NULL)
	{ 
		Shop_Setinfo::set_open($shop_id,$status,$reason_key); 
		$query_type = $this->request->query('query'); 
		$search = $this->request->query('search'); 
		$url='/audit?query='.$query_type.'&search='.$search;
		$this->request->redirect($url,302);
	}
	
	/**
	 * 获得商铺信息
	 * @param array $query 查询条件
	 * @param unknown_type $start 开始的记录，第一条从0开始
	 * @param unknown_type $limit 查询的记录数
	 * @return 商铺信息数组
	 */
	private function get_shops(array $query,$start,$limit)
	{
		$arr_result = Shop::search($query,$start,$limit);
		if(!empty($arr_result) && is_array($arr_result['list']) && count($arr_result['list']) > 0)
		{
			// 加工数组
			$arr_shops= &$arr_result['list'];
			foreach ($arr_shops as $key=>$value)
			{
				if($arr_shops[$key])
				{
					//根据省、市、地区、商圈、卖场id获取区域信息
					$arr_location=array();
					$arr_location[] = City::province_name(intval($arr_shops[$key]['province']));
					$arr_location[] = City::city_name(intval($arr_shops[$key]['city']));		
					$arr_location= array_unique($arr_location);
					$arr_location[] = City::county_name(intval($arr_shops[$key]['county']));	
					$arr_location[] = City::shangquan_name(intval($arr_shops[$key]['shangquan']));
					$arr_location[] = City::mall_name(intval($arr_shops[$key]['market']));
					$arr_shops[$key]['location'] = trim(implode(' ',$arr_location)); 
					//获取品牌名称
					$cate_brand = new Category_brand();
				    $arr_brandinfo = $arr_shops[$key]['brand'] ? $cate_brand->get_info($arr_shops[$key]['brand']) : '';
					$arr_shops[$key]['brand']=$arr_brandinfo ? $arr_brandinfo['name'] : '';
					//获取状态信息
				    $arr_shops[$key]['open_status'] = $this->get_status_name($arr_shops[$key]['open_status']);
				    //格式化申请时间
				    $arr_shops[$key]['apply_time'] = date('Y-m-d H:i:s',$arr_shops[$key]['apply_time']);
				    //获取主营行业
				    $array_major_ids  =Shop::major_categories($arr_shops[$key]['id']);
				    $array_major_name=Category_System::names($array_major_ids) ;
				    foreach ($array_major_name as $value)
				    {
				    	$arr_shops[$key]['major_category_name'] =  $value.' '.$arr_shops[$key]['major_category_name'] ;
				    }
				    //获取店铺地址
				    $arr_shops[$key]['shop_url'] = Shop::shop_detail_url($arr_shops[$key]['id']);
				}
			}
		}
		return $arr_result;
	}
	
	/**
	 * 返回关闭店铺理由数组
	 * @return array
	 */
	private function get_reasons()
	{
		$arr_reason = Kohana::config('shop.shop_closed_reason');
		return $arr_reason;
	}
	
	/**
	 * 根据店铺状态id，获得店铺状态字符串
	 * @param string $shop_status 店铺状态id
	 * @return string 店铺状态字符串
	 */
	private function get_status_name($shop_status)
	{
		$result='';
		foreach (Kohana::config('shop.open_status') as $key=>$value )
		{
			$shop_status==$value && $result=$key;
		}
		return $result;
	}
}
